package com.jjb.www.ui.activity;

import android.view.View;

import com.hjq.base.BaseDialog;
import com.hjq.shape.layout.ShapeLinearLayout;
import com.hjq.shape.view.ShapeButton;
import com.hjq.shape.view.ShapeTextView;
import com.jjb.www.R;
import com.jjb.www.aop.SingleClick;
import com.jjb.www.app.AppActivity;
import com.jjb.www.http.glide.GlideApp;
import com.jjb.www.manager.ActivityManager;
import com.jjb.www.manager.CacheDataManager;
import com.jjb.www.manager.ThreadPoolManager;
import com.jjb.www.other.AppConfig;
import com.jjb.www.ui.dialog.CommonUseDialog;

/**
 *    author : Android CRG
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/03/01
 *    desc   : 设置界面
 */
public final class SettingActivity extends AppActivity {
    private ShapeTextView mTvCleanCacheView;
    private ShapeButton mBtnLogin;
    private ShapeLinearLayout mLlCler;
    private ShapeLinearLayout mLlProblemFeedback;
    private ShapeLinearLayout mLlAboutUs;
    private ShapeLinearLayout mLlUserAgreement;
    private ShapeLinearLayout mLlPrivacyPolicy;
    private ShapeLinearLayout mLlAccountSettings;
    private ShapeLinearLayout mLlPushSettings;
    private ShapeLinearLayout mLlVersion;

    @Override
    protected int getLayoutId() {
        return R.layout.setting_activity;
    }

    @Override
    protected void initView() {
        mBtnLogin = findViewById(R.id.btn_login);
        mLlCler = findViewById(R.id.ll_cler);
        mTvCleanCacheView = findViewById(R.id.tv_cleanCacheView);

        mLlProblemFeedback = findViewById(R.id.ll_problem_feedback);
        mLlAboutUs = findViewById(R.id.ll_about_us);
        mLlUserAgreement = findViewById(R.id.ll_user_agreement);
        mLlPrivacyPolicy = findViewById(R.id.ll_privacy_policy);
        mLlAccountSettings = findViewById(R.id.ll_account_settings);
        mLlPushSettings = findViewById(R.id.ll_push_settings);
        mLlVersion = findViewById(R.id.ll_version);

        setOnClickListener(mBtnLogin,mLlCler,mLlProblemFeedback,mLlAboutUs,mLlUserAgreement,mLlPrivacyPolicy,mLlAccountSettings,mLlPushSettings,mLlVersion);
//        // 设置切换按钮的监听
//        mAutoSwitchView.setOnCheckedChangeListener(this);
//
//        setOnClickListener(R.id.sb_setting_language, R.id.sb_setting_update, R.id.sb_setting_phone,
//                R.id.sb_setting_password, R.id.sb_setting_agreement, R.id.sb_setting_about,
//                R.id.sb_setting_cache, R.id.sb_setting_auto, R.id.sb_setting_exit);
    }

    @Override
    protected void initData() {
        // 获取应用缓存大小
        mTvCleanCacheView.setText(CacheDataManager.getTotalCacheSize(this));

    }

    @SingleClick
    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.btn_login) {
            new CommonUseDialog.Builder(getContext())
                    .setHint("是否退出登录？")
                    .setLeftContent("取消")
                    .setRightContent("退出登录")
                    .setListener(new CommonUseDialog.OnListener() {
                        @Override
                        public void onCompleted(BaseDialog dialog) {

                        }

                        @Override
                        public void onCancel(BaseDialog dialog) {
                            AppConfig.getInstance().clearLoginInfo();
                            startActivity(LoginActivity.class);
                            // 进行内存优化，销毁除登录页之外的所有界面
                            ActivityManager.getInstance().finishAllActivities(LoginActivity.class);
                        }
                    }).show();
        }else if (viewId==R.id.ll_cler){
            GlideApp.get(getActivity()).clearMemory();
            ThreadPoolManager.getInstance().execute(() -> {
                CacheDataManager.clearAllCache(this);
                // 清除本地缓存（必须在子线程）
                GlideApp.get(getActivity()).clearDiskCache();
                post(() -> {
                    // 重新获取应用缓存大小
                    mTvCleanCacheView.setText(CacheDataManager.getTotalCacheSize(getActivity()));
                });
            });
        }else if (viewId==R.id.ll_problem_feedback){
            startActivity(ProblemFeedbackActivity.class);
        }else if (viewId==R.id.ll_about_us){
            startActivity(AboutActivity.class);
        }else if (viewId==R.id.ll_user_agreement){

        }else if (viewId==R.id.ll_privacy_policy){

        }else if (viewId==R.id.ll_account_settings){
            startActivity(LogoutUserActivity.class);
        }else if (viewId==R.id.ll_push_settings){

        }else if (viewId==R.id.ll_version){

        }
//
//            // 底部选择框
//            new MenuDialog.Builder(this)
//                    // 设置点击按钮后不关闭对话框
//                    //.setAutoDismiss(false)
//                    .setList(R.string.setting_language_simple, R.string.setting_language_complex)
//                    .setListener((MenuDialog.OnListener<String>) (dialog, position, string) -> {
//                        mLanguageView.setRightText(string);
//                        BrowserActivity.start(getActivity(), "https://github.com/getActivity/MultiLanguages");
//                    })
//                    .setGravity(Gravity.BOTTOM)
//                    .setAnimStyle(BaseDialog.ANIM_BOTTOM)
//                    .show();
//
//        } else if (viewId == R.id.sb_setting_update) {
//
//            // 本地的版本码和服务器的进行比较
//            if (20 > AppConfig.getVersionCode()) {
//                new UpdateDialog.Builder(this)
//                        .setVersionName("2.0")
//                        .setForceUpdate(false)
//                        .setUpdateLog("修复Bug\n优化用户体验")
//                        .setDownloadUrl("https://down.qq.com/qqweb/QQ_1/android_apk/Android_8.5.0.5025_537066738.apk")
//                        .setFileMd5("560017dc94e8f9b65f4ca997c7feb326")
//                        .show();
//            } else {
//                toast(R.string.update_no_update);
//            }
//
//        } else if (viewId == R.id.sb_setting_phone) {
//
//            new SafeDialog.Builder(this)
//                    .setListener((dialog, phone, code) -> PhoneResetActivity.start(getActivity(), code))
//                    .show();
//
//        } else if (viewId == R.id.sb_setting_password) {
//
//            new SafeDialog.Builder(this)
//                    .setListener((dialog, phone, code) -> PasswordResetActivity.start(getActivity(), phone, code))
//                    .show();
//
//        } else if (viewId == R.id.sb_setting_agreement) {
//
//            BrowserActivity.start(this, "https://github.com/getActivity/Donate");
//
//        } else if (viewId == R.id.sb_setting_about) {
//
//            startActivity(AboutActivity.class);
//
//        } else if (viewId == R.id.sb_setting_auto) {
//
//            // 自动登录
//            mAutoSwitchView.setChecked(!mAutoSwitchView.isChecked());
//
//        } else if (viewId == R.id.sb_setting_cache) {
//

//        } else if (viewId == R.id.sb_setting_exit) {
//
//            if (true) {
//                startActivity(LoginActivity.class);
//                // 进行内存优化，销毁除登录页之外的所有界面
//                ActivityManager.getInstance().finishAllActivities(LoginActivity.class);
//                return;
//            }
//
//            // 退出登录
//            EasyHttp.post(this)
//                    .api(new LogoutApi())
//                    .request(new HttpCallback<HttpData<Void>>(this) {
//
//                        @Override
//                        public void onSucceed(HttpData<Void> data) {
//                            startActivity(LoginActivity.class);
//                            // 进行内存优化，销毁除登录页之外的所有界面
//                            ActivityManager.getInstance().finishAllActivities(LoginActivity.class);
//                        }
//                    });
//
//        }
    }

}